# config.py
import os
from dotenv import load_dotenv

load_dotenv()


class Config:
    # 确保使用兼容的数据库URL
    SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL', 'sqlite:///news_ai.db')

    # 添加数据库特定配置
    SQLALCHEMY_ENGINE_OPTIONS = {
        'pool_pre_ping': True,
        'pool_recycle': 300,
    }

    SQLALCHEMY_TRACK_MODIFICATIONS = False

    # 百度AI配置
    BAIDU_AI_API_KEY = os.getenv('BAIDU_AI_API_KEY')
    BAIDU_AI_SECRET_KEY = os.getenv('BAIDU_AI_SECRET_KEY')

    # DeepSeek配置
    DEEPSEEK_API_KEY = os.getenv('DEEPSEEK_API_KEY')
    DEEPSEEK_API_URL = os.getenv('DEEPSEEK_API_URL', 'https://api.deepseek.com/v1/chat/completions')
    DEEPSEEK_MODEL = os.getenv('DEEPSEEK_MODEL', 'deepseek-chat')

    # AI功能开关
    BAIDU_SUMMARY_ENABLED = os.getenv('BAIDU_SUMMARY_ENABLED', 'True').lower() == 'true'
    DEEPSEEK_SUMMARY_ENABLED = os.getenv('DEEPSEEK_SUMMARY_ENABLED', 'False').lower() == 'true'
    ENABLE_FALLBACK_SUMMARY = os.getenv('ENABLE_FALLBACK_SUMMARY', 'True').lower() == 'true'

    # 百度AI参数配置
    BAIDU_MAX_CONTENT_LENGTH = int(os.getenv('BAIDU_MAX_CONTENT_LENGTH', '2000'))
    BAIDU_MAX_SUMMARY_LENGTH = int(os.getenv('BAIDU_MAX_SUMMARY_LENGTH', '200'))

    # 备用摘要配置
    FALLBACK_MAX_SENTENCES = int(os.getenv('FALLBACK_MAX_SENTENCES', '3'))
    FALLBACK_MAX_CHARS = int(os.getenv('FALLBACK_MAX_CHARS', '100'))

    # 看板摘要配置
    BOARD_SUMMARY_MAX_LENGTH = int(os.getenv('BOARD_SUMMARY_MAX_LENGTH', '15'))
    BOARD_SUMMARY_ELLIPSIS = os.getenv('BOARD_SUMMARY_ELLIPSIS', '...')

    # Celery配置
    CELERY_BROKER_URL = os.getenv('CELERY_BROKER_URL', 'redis://localhost:6379/0')
    CELERY_RESULT_BACKEND = os.getenv('CELERY_RESULT_BACKEND', 'redis://localhost:6379/0')

    # 评分配置
    ENTITY_WEIGHTS = {
        '博世': 10,
        '大陆': 10,
        '电装': 10,
        '华为': 8,
        'XM': 8,
        'LP': 8,
        'XP': 8,
        'Geely': 8,
        'LX': 8,
        '特斯拉':8,
        '其他OEM': 5,
        '竞争对手': 7,
        'default': 1
    }

    CATEGORY_WEIGHTS = {
        '全球热点': 1.5,
        '重点客户': 1.3,
        '其他OEM': 1.0,
        '竞争对手': 1.2,
        '产品信息': 1.1,
        '其他': 1.0
    }

    TIME_BASE_SCORE = int(os.getenv('TIME_BASE_SCORE', '24'))
    ENTITY_WEIGHT_MULTIPLIER = float(os.getenv('ENTITY_WEIGHT_MULTIPLIER', '1.0'))
    TIME_WEIGHT_MULTIPLIER = float(os.getenv('TIME_WEIGHT_MULTIPLIER', '1.0'))
    LENGTH_WEIGHT_MULTIPLIER = float(os.getenv('LENGTH_WEIGHT_MULTIPLIER', '0.1'))

class CeleryConfig:
    # Celery配置
    broker_url = os.getenv('CELERY_BROKER_URL', 'redis://localhost:6379/0')
    result_backend = os.getenv('CELERY_RESULT_BACKEND', 'redis://localhost:6379/0')
    task_serializer = 'json'
    accept_content = ['json']
    result_serializer = 'json'
    timezone = 'UTC'
    enable_utc = True
    task_routes = {
        'celery_worker.process_news_task': {'queue': 'news_processing'},
        'celery_worker.update_news_scores': {'queue': 'scheduled_tasks'},
    }

# 实例化配置对象
config = Config()
celery_config = CeleryConfig()
